package com.movie.controller;

import com.movie.dto.RatingRequest;
import com.movie.dto.ResponseDTO;
import com.movie.entity.Rating;
import com.movie.service.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rating")
public class RatingController {

    @Autowired
    private RatingService ratingService;

    /**
     * 用户评分接口（可以包含评论）
     */
    @PostMapping
    public ResponseDTO<Void> addOrUpdateRating(
            @RequestHeader("X-User-Id") Integer userId,
            @RequestBody RatingRequest request) {

        if (request.getScore() < 1 || request.getScore() > 5) {
            return ResponseDTO.error("评分必须在1~5之间");
        }

        Rating rating = new Rating();
        rating.setUserId(userId);
        rating.setMovieId(request.getMovieId());
        rating.setScore(request.getScore());
        rating.setComment(request.getComment()); // 支持评论

        ratingService.addOrUpdateRating(rating);
        return ResponseDTO.success(null, "评分成功");
    }

    /**
     * 获取某电影的所有评分（含评论）
     */
    @GetMapping("/movie/{movieId}")
    public ResponseDTO<List<Rating>> getRatingsByMovie(@PathVariable Integer movieId) {
        List<Rating> ratings = ratingService.getRatingsByMovie(movieId);
        return ResponseDTO.success(ratings, "获取评分列表成功");
    }

    /**
     * 获取某用户的所有评分（含评论）
     */
    @GetMapping("/user/{userId}")
    public ResponseDTO<List<Rating>> getRatingsByUser(@PathVariable Integer userId) {
        List<Rating> ratings = ratingService.getRatingsByUser(userId);
        return ResponseDTO.success(ratings, "获取用户评分成功");
    }
}
